package com.gnrsoft.rpc.common;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 
 * <B>中文类名：Rpc 编码器</B><BR>
 * <B>概要说明：</B><BR>
 * @author likun
 * @since 2017年7月19日
 */
public class RpcEncoder extends MessageToByteEncoder {

	private Class<?> genericClass;

	public RpcEncoder(Class<?> classz) {
		this.genericClass = classz;
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {

		if (genericClass.isInstance(msg)) {
			byte[] data = KryoUtil.writeToByteArray(msg);
			out.writeInt(data.length);
			out.writeBytes(data);
		}

	}

}
